Skip to content

fix: prevent two OOB crashes in updateAnnotatedString (Samsung soft keyboard + large text shrink)#721

Closed
tfkci wants to merge 1 commit into
MohamedRejeb:mainfrom
tfkci:fix/upstream-oob-crashes
Closed

fix: prevent two OOB crashes in updateAnnotatedString (Samsung soft keyboard + large text shrink)#721
tfkci wants to merge 1 commit into
MohamedRejeb:mainfrom
tfkci:fix/upstream-oob-crashes

Conversation

@tfkci

@tfkci tfkci commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Two OutOfBoundsException crashes in updateAnnotatedString and its call chain, both reproducible on Samsung budget/mid-range devices (Galaxy A04, A57, M-series) and physical keyboards.

Bug 1 — IndexOutOfBoundsException: index: N, size: N

Location: RichTextState.updateAnnotatedString

richParagraphList.removeAt(i) was called inside fastForEachIndexed, which captures the list size once at loop entry. After the first removal the list shrinks but the loop index keeps incrementing, eventually accessing an index beyond the new size.

Fix: Collect stale paragraph indices during the loop (paragraphIndicesToRemove) and remove them in reverse order after buildAnnotatedString completes.

Bug 2 — StringIndexOutOfBoundsException: begin N, end M, length L (M > L)

Location: AnnotatedStringExt.kttext.substring(index, index + richSpan.text.length)

Samsung soft keyboards on budget devices route certain operations (autocomplete, autocorrect, word suggestions) through TextFieldKeyInput (dispatchKeyEvent) rather than the standard IME protocol. This delivers a new TextFieldValue atomically before span reconciliation runs, so a span's stale stored length can exceed the incoming text length. Physical keyboards connected to Android hit the same code path.

Typical stack:

StringIndexOutOfBoundsException: begin 132, end 146, length 144
  at AnnotatedStringExt.append (AnnotatedStringExt.kt:142)
  at RichTextState.updateAnnotatedString (RichTextState.kt:...)
  at RichTextState.updateTextFieldValue (RichTextState.kt:...)
  at RichTextState.onTextFieldValueChangeInner (RichTextState.kt:...)
  at TextFieldKeyInput.apply (TextFieldKeyInput.kt:69)   ← soft keyboard via dispatchKeyEvent

Fix: Clamp safeStart and safeEnd to text.length before the substring call so stale span lengths are handled gracefully.

Test plan

  • RichTextStateParagraphRemovalTest — three new regression cases covering both crash paths
  • All existing tests pass (./gradlew :richeditor-compose:desktopTest)

Devices known to trigger Bug 2

Samsung Galaxy A04, A57 (and likely other Samsung A/M-series budget phones using Samsung Keyboard with word suggestions enabled).

**Bug 1 — IndexOutOfBoundsException in updateAnnotatedString**

`richParagraphList.removeAt(i)` was called inside `fastForEachIndexed`,
which captures the list size once at loop entry.  After the first removal
the list shrinks but the loop index keeps incrementing, eventually
accessing an index beyond the new size and throwing.

Fix: collect stale paragraph indices during the loop and remove them in
reverse order after `buildAnnotatedString` completes.

**Bug 2 — StringIndexOutOfBoundsException in AnnotatedStringExt**

`text.substring(index, index + richSpan.text.length)` assumed the span's
stored length never exceeds the available text.  Samsung soft keyboards on
budget devices (Galaxy A04, A57, and similar) route certain input events —
autocomplete, autocorrect, word suggestions — through `TextFieldKeyInput`
(dispatchKeyEvent) rather than the standard IME protocol.  This delivers a
new `TextFieldValue` atomically before span reconciliation runs, so a
span's stale stored length can exceed the incoming text length → crash.
Physical keyboards connected to Android trigger the same code path.

Fix: clamp `safeStart` and `safeEnd` to `text.length` before the
`substring` call so stale span lengths are handled gracefully.

Adds `RichTextStateParagraphRemovalTest` with three regression cases
covering both crash paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ErnestoOlalla

Copy link
Copy Markdown

+1 — this issue is affecting us as well. This PR would be very helpful.

@ErnestoOlalla

Copy link
Copy Markdown

Cross-linking a separate editor crash for anyone landing here: on Compose UI 1.12+, long-pressing text in BasicRichTextEditor throws IllegalArgumentException from require(this is AndroidClipboard) (foundation's paste-availability path) — tracked in #744, with a fix in #745.

It's unrelated to the OOB fixes in this PR, but they hit the same component: consumers moving to the latest Compose currently need both this and #745. Would be great to see this one merged too.

@MohamedRejeb

Copy link
Copy Markdown
Owner

Both crashes belonged to the same tree/text desync family that #746 fixed at the root, and that whole family is now covered by fuzz tests. The stack traces in your report were genuinely helpful in pinning it down, thanks. Closing as fixed by #746.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants